package com.key.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.EndAssetsFindDTO;
import com.key.common.vo.AssetListVO;
import com.key.common.vo.AssetsVO;
import com.key.mapper.AssetsMapper;
import com.key.mapper.EndAssetsMapper;
import com.key.mapper.ShowBackMapper;
import com.key.service.EndAssetsService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.ShowBackUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
 * @description:到期资产相关业务
 * @author: jianjiawei
 * @date: 2023/2/22 10:36
 **/
@Service
public class EndAssetsServiceImpl implements EndAssetsService {
    @Autowired
    EndAssetsMapper endAssetsMapper;
    @Autowired
    AssetsMapper assetsMapper;
    @Autowired
    ShowBackUtils showBackUtils;

    /*
     *
     * @description:查询使用到期资产列表
     * @author: jianjiawei
     * @date: 2023/2/22 10:36
     **/
    @Override
    public R findEndAssetsList(Integer currentPage, Integer pageSize) {
        /*if (currentPage == null){
            currentPage = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }*/
        R r = new R();
        PageHelper.startPage(currentPage, pageSize);
        try {
            List<AssetListVO> assetsVOList = endAssetsMapper.selectEndAssetsList();
            if (assetsVOList != null & assetsVOList.size() > 0) {
                //取出结果集的所用资产id
                List<Long> assetsIds = new ArrayList<>();
                for (AssetListVO assetsVO : assetsVOList) {
                    assetsIds.add(assetsVO.getId());
                }
                //根据资产id查询所属公司
                List<Map<String, Object>> assetsAdminList = assetsMapper.getAssetsAdmin(assetsIds);
                //把所属公司赋值给资产视图对象的所属公司
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsAdminList) {
                        if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                            assetsVO.setAssetsAdmin((String) map.get("companyName"));
                        }
                    }
                }
                //根据资产id查询管理员
                List<Map<String, Object>> assetsManagerList = assetsMapper.getAssetsManager(assetsIds);
                //把管理员赋值给资产视图对象的管理员
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsManagerList) {
                        if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                            assetsVO.setAssetsManager((String) map.get("employeeName"));
                        }
                    }
                }
                //根据资产id查询创建者
                List<Map<String, Object>> assetsCreaterList = assetsMapper.getAssetsCreater(assetsIds);
                for (Map<String, Object> stringObjectMap : assetsCreaterList) {
                    System.out.println(stringObjectMap);

                }
                //把创建者赋值给资产视图对象的创建者
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsCreaterList) {
                        if (assetsVO.getId() == map.get("assets_id") || assetsVO.getId().equals(map.get("assets_id"))) {
                            assetsVO.setAssetsCreater((String) map.get("employeeName"));
                        }
                    }
                }
                String data = JSONObject.toJSONString(assetsVOList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器内部错误");
        }
        return r;
    }

    @Override
    public R findEndAssetsListByAdv(EndAssetsFindDTO findDTO) {

        R r = new R();
        PageHelper.startPage(findDTO.getCurrentPage(), findDTO.getPageSize());
        try {
            List<AssetListVO> assetsVOList = endAssetsMapper.selectEndEndAssetsListByAdv(findDTO);
            if (assetsVOList != null & assetsVOList.size() > 0) {
                //取出结果集的所用资产id
                List<Long> assetsIds = new ArrayList<>();
                for (AssetListVO assetsVO : assetsVOList) {
                    assetsIds.add(assetsVO.getId());
                }
                //根据资产id查询所属公司
                List<Map<String, Object>> assetsAdminList = assetsMapper.getAssetsAdmin(assetsIds);
                //把所属公司赋值给资产视图对象的所属公司
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsAdminList) {
                        if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                            assetsVO.setAssetsAdmin((String) map.get("companyName"));
                        }
                    }
                }
                //根据资产id查询管理员
                List<Map<String, Object>> assetsManagerList = assetsMapper.getAssetsManager(assetsIds);
                //把管理员赋值给资产视图对象的管理员
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsManagerList) {
                        if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                            assetsVO.setAssetsManager((String) map.get("employeeName"));
                        }
                    }
                }
                //根据资产id查询创建者
                List<Map<String, Object>> assetsCreaterList = assetsMapper.getAssetsCreater(assetsIds);
                //把创建者赋值给资产视图对象的创建者
                for (AssetListVO assetsVO : assetsVOList) {
                    for (Map<String, Object> map : assetsCreaterList) {
                        if (assetsVO.getId() == map.get("assets_id") || assetsVO.getId().equals(map.get("assets_id"))) {
                            assetsVO.setAssetsCreater((String) map.get("employeeName"));
                        }
                    }
                }
                String data = JSONObject.toJSONString(assetsVOList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(data);
                return r;
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器内部错误");
        }
        return r;
    }

    /*
     *
     * @description:高级搜索回显
     * @author: jianjiawei
     * @date: 2023/2/24 15:53
     **/
    @Override
    public R findByAdvShowBack() {
        List<String> companies = showBackUtils.showCompanies();
        List<String> models = showBackUtils.showAssetModels();
        List<String> regions = showBackUtils.showRegions();
        List<String> users = showBackUtils.showUsers();
        List<String> types = showBackUtils.showAssetTypes();
        List<String> departments = showBackUtils.showDepartments();
        Map map = new HashMap();
        map.put("companyList", companies);
        map.put("modelList", models);
        map.put("userList", users);
        map.put("regionList", regions);
        map.put("departments", departments);
        map.put("types", types);
        String data = JSONObject.toJSONString(map);
        System.out.println(data);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    /*
     *
     * @description:得到Excel工作簿
     * @author: jianjiawei
     * @date: 2023/2/24 19:31
     **/
    @Override
    public HSSFWorkbook getWorkBook() {
        String[] header = {"主键ID", "变更状态", "签字状态", "资产照片", "资产编码", "资产名称", "RFID", "资产类别", "资产型号",
                "资产标签", "SN号", "计量单位", "资产金额", "使用公司", "使用部门", "使用人", "资产区域", "资产存放地", "管理员", "所属公司",
                "购入日期", "清理日期", "维保到期", "供应商", "使用期限", "来源", "创建者", "创建时间", "收货单号"};
        String[] properties = {"id", "changeState", "signState", "assetsPhoto", "assetsCode", "assetName", "assetsRFID",
                "assetClass", "assetModel", "label", "assetsSerialNumber", "assetsUnitMeasurement", "assetMoney", "assetUserComp",
                "assetDept", "assetUser", "assetsRegionName", "assetsStorageLocation", "assetsManager", "assetsAdmin", "assetsPurchaseDate",
                "assetsClearDate", "assetsWBEndDate", "supplier", "assetsServiceLife", "assetsSource", "assetsCreater", "assetsCreateTime",
                "assetsReceiptNo"};
        List<AssetListVO> assetsVOList = endAssetsMapper.selectEndAssetsList();
        HSSFWorkbook workbook = null;
        if (assetsVOList != null & assetsVOList.size() > 0) {
            //取出结果集的所用资产id
            List<Long> assetsIds = new ArrayList<>();
            for (AssetListVO assetsVO : assetsVOList) {
                assetsIds.add(assetsVO.getId());
            }
            //根据资产id查询所属公司
            List<Map<String, Object>> assetsAdminList = assetsMapper.getAssetsAdmin(assetsIds);
            //把所属公司赋值给资产视图对象的所属公司
            for (AssetListVO assetsVO : assetsVOList) {
                for (Map<String, Object> map : assetsAdminList) {
                    if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                        assetsVO.setAssetsAdmin((String) map.get("companyName"));
                    }
                }
            }
            //根据资产id查询管理员
            List<Map<String, Object>> assetsManagerList = assetsMapper.getAssetsManager(assetsIds);
            //把管理员赋值给资产视图对象的管理员
            for (AssetListVO assetsVO : assetsVOList) {
                for (Map<String, Object> map : assetsManagerList) {
                    if (assetsVO.getId().equals((Long) map.get("assets_id"))) {
                        assetsVO.setAssetsManager((String) map.get("employeeName"));
                    }
                }
            }
            //根据资产id查询创建者
            List<Map<String, Object>> assetsCreaterList = assetsMapper.getAssetsCreater(assetsIds);
            for (Map<String, Object> stringObjectMap : assetsCreaterList) {
                System.out.println(stringObjectMap);

            }
            //把创建者赋值给资产视图对象的创建者
            for (AssetListVO assetsVO : assetsVOList) {
                for (Map<String, Object> map : assetsCreaterList) {
                    if (assetsVO.getId() == map.get("assets_id") || assetsVO.getId().equals(map.get("assets_id"))) {
                        assetsVO.setAssetsCreater((String) map.get("employeeName"));
                    }
                }
            }
            List list = new ArrayList();
            for (AssetListVO assetListVO : assetsVOList) {
                list.add(assetListVO);
            }
            ExcelExportHelper excelHelper = new ExcelExportHelper();
            workbook = excelHelper.exportExcel(header, properties, list, "到期资产列表");
        }
        return workbook;
    }

    /*
    *
     * @description:根据id清理资产
     * @author: jianjiawei
     * @date: 2023/2/25 11:02
     **/
    @Override
    @Transactional
    public R cleanAssetsByIds(List<Integer> ids) {
        R r = new R();
        int row = 0;
        if (ids != null & ids.size()>0){
            for (Integer id : ids) {
               row = endAssetsMapper.cleanAssetsById(id);
            }
        }else {
            r.setCode(201);
            r.setMsg("id数组为空");
        }
        if (row > 0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(400);
            r.setMsg("清理失败");
        }
        return r;
    }
}
